package controller;

import java.util.ArrayList;
import java.util.List;
import jpa.exceptions.NonexistentEntityException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.DaftarUser;
//import model.Users;
import model.User;

/**
 *
 * @author AgusNH
 */
public class LogicAkun {

    HttpServletRequest request;

    public LogicAkun(HttpServletRequest request) {
        this.request = request;
    }

    public String register() {
        User user = new User();
        String nama = request.getParameter("nama");
        String alamat = request.getParameter("alamat");
        String telepon = request.getParameter("telepon");
        String email = request.getParameter("email");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        user.setNama(nama);
        user.setAlamat(alamat);
        user.setTelepon(telepon);
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setAdminStat(0);
        //DbConnection dbConn = new DbConnection();
        //ModelAkun mdReg = new ModelAkun(dbConn.getConnection());
        DaftarUser du = new DaftarUser();
        //mdReg.insertUser(nama, alamat, telepon, email, username, password);
        du.addUser(user);
        return "index.jsp";
    }

    public String goProfil() throws Exception {
        HttpSession session = request.getSession();
        User user = new User();
        DaftarUser du = new DaftarUser();
        //DbConnection dbConn = new DbConnection();
        //ModelAkun mdLogin = new ModelAkun(dbConn.getConnection());
        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            user = du.getUser(username);
            if (user.getAdminStat() == 1) {
                request.setAttribute("user", user);
                //dbConn.closeConnection();
                return "profilAdmin.jsp";
            } else {
                request.setAttribute("user", user);
                //dbConn.closeConnection();
                return "profil.jsp";
            }
        } else {
            return "login.jsp";
        }
    }

    public String login() throws Exception {
        HttpSession session = request.getSession();
        User user = new User();
        DaftarUser du = new DaftarUser();
        //DbConnection dbConn = new DbConnection();
        //ModelAkun mdLogin = new ModelAkun(dbConn.getConnection());
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username.equals("") || password.equals("")) {
            return "login.jsp";
        } else {
            user = du.getUser(username);
            if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
                session.setAttribute("uname", username);
                if (user.getAdminStat() == 1) {
                    request.setAttribute("user", user);
                    //dbConn.closeConnection();
                    return "profilAdmin.jsp";
                } else {
                    request.setAttribute("user", user);
                    //dbConn.closeConnection();
                    return "profil.jsp";
                }
            } else {
                return "login.jsp";
            }
        }

    }

    public String logout() throws Exception {
        HttpSession session = request.getSession();
        session.removeAttribute("uname");
        session.invalidate();
        return "index.jsp";
    }

    public String goEditProfil() {
        String username = request.getParameter("username");
        User user = new User();
        DaftarUser du = new DaftarUser();
        //DbConnection dbConn = new DbConnection();
        //ModelAkun mdEdit = new ModelAkun(dbConn.getConnection());
        user = du.getUser(username);
        request.setAttribute("user", user);
        return "editProfil.jsp";
    }

    public String updateProfil() throws Exception {
        HttpSession session = request.getSession();
        User user = new User();
        String nama = request.getParameter("nama");
        String alamat = request.getParameter("alamat");
        String telepon = request.getParameter("telepon");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        user.setNama(nama);
        user.setAlamat(alamat);
        user.setTelepon(telepon);
        user.setEmail(email);
        user.setPassword(password);

        DaftarUser du = new DaftarUser();
        //DbConnection dbConn = new DbConnection();
        //ModelAkun mdEdit = new ModelAkun(dbConn.getConnection());

        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            du.editUser(user);
            //user = mdEdit.getUser(username);
            if (user.getAdminStat() == 1) {
                request.setAttribute("user", user);
                //dbConn.closeConnection();
                return "profilAdmin.jsp";
            } else {
                request.setAttribute("user", user);
                //dbConn.closeConnection();
                return "profil.jsp";
            }
        } else {
            return "login.jsp";
        }

    }

    public String deleteUser() throws NonexistentEntityException {
        String idUser = request.getParameter("idUser");
        List<User> user = new ArrayList<User>();
        DaftarUser du = new DaftarUser();
        //DbConnection dbConn = new DbConnection();
        //ModelAkun mdHap = new ModelAkun(dbConn.getConnection());

        if (idUser == null) {
            user = du.getUsers();
            request.setAttribute("users", user);
        } else {
            int id = Integer.parseInt(idUser);
            du.deleteUser(id);
            user = du.getUsers();
            request.setAttribute("users", user);
        }

        return "listUsers.jsp";
    }
}
